package com.bw.kf.all2211a.seatch

import android.os.Bundle

import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.GridLayoutManager
import com.bw.kf.all2211a.R
import com.bw.kf.all2211a.adapter.ProductAdapter
import com.bw.kf.all2211a.databinding.ActivitySearchBinding
import com.bw.kf.mvvm.base.BaseActivity

class SearchActivity : BaseActivity<ActivitySearchBinding,SearchViewMode>() {
    lateinit var productAdapter: ProductAdapter
    override fun iniView() {
        productAdapter=ProductAdapter()
        binding.rv.adapter=productAdapter
        binding.rv.layoutManager=GridLayoutManager(this,2)

        binding.sv.setOnQueryTextListener(object : SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                if(query!=null){

                    viewModel.search(0,query.toString(),20,false,1,"","")

                }
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                if(newText!=null){
                    viewModel.search(0,newText.toString(),20,false,1,"","")
                }
                return false
            }

        })
    }

    override fun iniData() {
                   viewModel.success.observe(this){
                       if(it.code==200){
                           productAdapter.data.clear()
                           productAdapter.data.addAll(it.data.list)
                           productAdapter.notifyDataSetChanged()
                       }
                   }
    }

    override fun getViewModel(): Class<SearchViewMode> {
      return SearchViewMode::class.java
    }

    override fun Layoutid(): Int {
       return R.layout.activity_search
    }

}